const countdownApi = require('../../utils/countdownApi');

Page({
  data: {
    testResult: '点击上方按钮开始测试API接口...'
  },

  onLoad: function (options) {
    console.log('API测试页面加载');
  },

  // 测试获取列表
  async testGetList() {
    this.setData({ testResult: '正在测试获取列表...' });
    
    try {
      const response = await countdownApi.getList();
      this.setData({
        testResult: `获取列表成功：\n${JSON.stringify(response, null, 2)}`
      });
    } catch (error) {
      this.setData({
        testResult: `获取列表失败：\n${error.message}`
      });
    }
  },

  // 测试获取统计
  async testGetStatistics() {
    this.setData({ testResult: '正在测试获取统计...' });
    
    try {
      const response = await countdownApi.getStatistics();
      this.setData({
        testResult: `获取统计成功：\n${JSON.stringify(response, null, 2)}`
      });
    } catch (error) {
      this.setData({
        testResult: `获取统计失败：\n${error.message}`
      });
    }
  },

  // 测试添加事件
  async testAdd() {
    this.setData({ testResult: '正在测试添加事件...' });
    
    try {
      const testData = {
        eventName: '测试倒数日事件',
        targetDate: '2024-12-31',
        creator: '测试用户'
      };
      
      const response = await countdownApi.add(testData);
      this.setData({
        testResult: `添加事件成功：\n${JSON.stringify(response, null, 2)}`
      });
    } catch (error) {
      this.setData({
        testResult: `添加事件失败：\n${error.message}`
      });
    }
  },

  // 测试搜索
  async testSearch() {
    this.setData({ testResult: '正在测试搜索...' });
    
    try {
      const response = await countdownApi.search('测试');
      this.setData({
        testResult: `搜索成功：\n${JSON.stringify(response, null, 2)}`
      });
    } catch (error) {
      this.setData({
        testResult: `搜索失败：\n${error.message}`
      });
    }
  }
});

